﻿using System;
using System.Collections.Generic;
using System.Linq;
using TuxedoLib;

namespace SpaceSquirrel
{
	public class CreditsScene : GameSceneBase
	{
		private int counter;

		private static readonly Pair<string, string[]>[] CREDITS = new Pair<string, string[]>[] {
			new Pair<string, string[]>("TEAM NERD PARADISE", new string[0]),
			new Pair<string, string[]>("PROGRAMMING", new string[] { "BLAKE O'HARE" }),
			new Pair<string, string[]>("LEVEL DESIGN", new string[] { "TED BURTON", "STEVE CRAWFORD", "LAURA FREER" }),
			new Pair<string, string[]>("CHARACTER ART", new string[] { "CHRISTINE SANDQUIST" }),
			new Pair<string, string[]>("LEVEL ART", new string[] { "ANGEL MCLAUGHLIN" }),
			new Pair<string, string[]>("MUSIC AND SFX", new string[] { "ADRIAN CLINE" }),
			new Pair<string, string[]>("DIALOG", new string[] { "LAURA FREER" }),
			new Pair<string, string[]>("AND YOU!", new string[] { "FOR PLAYING", "OR SOMETHING LIKE THAT" }),
			new Pair<string, string[]>("", new string[] { "THE END" })
		};

		private Image[] images;
		private int[] yPixels;

		public CreditsScene()
			: base()
		{
			this.counter = 0;
			List<Image> images = new List<Image>();
			List<int> yPixels = new List<int>();

			int y = 0;
			for (int i = 0; i < CREDITS.Length; ++i)
			{
				images.Add(Text.Render(-1, CREDITS[i].First));
				yPixels.Add(y);

				y += 16;
				foreach (string person in CREDITS[i].Second)
				{
					images.Add(Text.Render(-1, person));
					yPixels.Add(y);
					y += 8;
				}

				y += 64;
			}

			this.images = images.ToArray();
			this.yPixels = yPixels.ToArray();
		}

		public override void ProcessInput(List<MyEvent> events, Dictionary<string, bool> pressedActions)
		{
			if (this.counter > 900)
			{
				foreach (MyEvent ev in events)
				{
					if (ev.Down)
					{
						if (ev.Action == "start" || ev.Action == "A" || ev.Action == "B")
						{
							this.Next = new TitleScene();
						}
					}
				}
			}
		}

		public override void Update()
		{
			this.PlayMusic();
			this.counter++;
		}

		public override void Render(TuxedoLib.Image screen, int renderCounter)
		{
			screen.Fill(0, 0, 0);
			int counter = Math.Min(this.counter, 892);

			for (int i = 0; i < this.images.Length; ++i)
			{
				screen.Blit(this.images[i], 64, this.yPixels[i] - counter + 224);
			}
		}

		private void PlayMusic()
		{
			JukeBox.EnsureSong("credits");
		}
	}
}
